package com.ymatou.iapi.userservice.testcase;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.ymatou.iapi.userservice.parameter.enums.ThirdPartyTypeEnum;
import com.ymatou.iapi.userservice.parameter.req.BindThirdPartyUserReq;
import com.ymatou.iapi.userservice.parameter.req.CreateUserReq;
import com.ymatou.iapi.userservice.parameter.req.UnBindThirdPartyUserReq;
import com.ymatou.iapi.userservice.service.GetUserThirdPartyAccountDtoCall;
import com.ymatou.userservice.parameter.GetUserThirdPartyAccountDtoBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.database.model.ThirdpartyUserbind;
import com.ymttest.database.sqlwapper.JUserWapper;

/**
 * 验证交易密码Test FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_GetUserThirdPartyAccountDto {
	private static GetUserThirdPartyAccountDtoBean getUserThirdPartyAccountDtoBean;
	private static GetUserThirdPartyAccountDtoCall getUserThirdPartyAccountDtoCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询第三方信息");
	}

	@Before
	public void caseUp() {
		getUserThirdPartyAccountDtoBean = new GetUserThirdPartyAccountDtoBean();
		getUserThirdPartyAccountDtoCall = new GetUserThirdPartyAccountDtoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getUserThirdPartyAccountDtoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetUserThirdPartyAccountDto_001() {
		Logger.start(true, "验证注册后绑定WeiXin第三方账号信息成功");
		try {
			CreateUserReq cur = new CreateUserReq();
			cur = JUserCallService.initCreateUserBean(cur);
			int uid = JUserCallService.createUserCall(cur);
			BindThirdPartyUserReq bd = JUserCallService.initBindThirdUserReq(uid);
			bd.setThirdPartyType(ThirdPartyTypeEnum.WeiXin);
			Map<String, BindThirdPartyUserReq> m = new HashMap<String, BindThirdPartyUserReq>();
			m.put(bd.getThirdPartyId(), bd);
			JUserCallService.bindThirdPartyUserCall(bd);
			getUserThirdPartyAccountDtoBean.setUserId(uid);
			getUserThirdPartyAccountDtoCall.setData(getUserThirdPartyAccountDtoBean);
			getUserThirdPartyAccountDtoCall.callService();

			Logger.verifyEquals("true", getUserThirdPartyAccountDtoCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", getUserThirdPartyAccountDtoCall.getString("ResponseCode"), "验证ResponseCode");

			verifyDB(uid, m, getUserThirdPartyAccountDtoCall);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetUserThirdPartyAccountDto_002() {
		Logger.start(true, "验证注册后绑定Weibo第三方账号信息成功");
		try {
			CreateUserReq cur = new CreateUserReq();
			cur = JUserCallService.initCreateUserBean(cur);
			int uid = JUserCallService.createUserCall(cur);
			BindThirdPartyUserReq bd = JUserCallService.initBindThirdUserReq(uid);
			bd.setThirdPartyType(ThirdPartyTypeEnum.Weibo);
			Map<String, BindThirdPartyUserReq> m = new HashMap<String, BindThirdPartyUserReq>();
			m.put(bd.getThirdPartyId(), bd);
			JUserCallService.bindThirdPartyUserCall(bd);
			getUserThirdPartyAccountDtoBean.setUserId(uid);
			getUserThirdPartyAccountDtoCall.setData(getUserThirdPartyAccountDtoBean);
			getUserThirdPartyAccountDtoCall.callService();

			Logger.verifyEquals("true", getUserThirdPartyAccountDtoCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", getUserThirdPartyAccountDtoCall.getString("ResponseCode"), "验证ResponseCode");
			verifyDB(uid, m, getUserThirdPartyAccountDtoCall);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetUserThirdPartyAccountDto_003() {
		Logger.start(true, "验证注册后绑定Alipay第三方账号信息成功");
		try {
			CreateUserReq cur = new CreateUserReq();
			cur = JUserCallService.initCreateUserBean(cur);
			int uid = JUserCallService.createUserCall(cur);
			BindThirdPartyUserReq bd = JUserCallService.initBindThirdUserReq(uid);
			bd.setThirdPartyType(ThirdPartyTypeEnum.Alipay);
			Map<String, BindThirdPartyUserReq> m = new HashMap<String, BindThirdPartyUserReq>();
			m.put(bd.getThirdPartyId(), bd);
			JUserCallService.bindThirdPartyUserCall(bd);
			getUserThirdPartyAccountDtoBean.setUserId(uid);
			getUserThirdPartyAccountDtoCall.setData(getUserThirdPartyAccountDtoBean);
			getUserThirdPartyAccountDtoCall.callService();

			Logger.verifyEquals("true", getUserThirdPartyAccountDtoCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", getUserThirdPartyAccountDtoCall.getString("ResponseCode"), "验证ResponseCode");
			verifyDB(uid, m, getUserThirdPartyAccountDtoCall);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetUserThirdPartyAccountDto_004() {
		Logger.start(true, "验证注册后绑定多个第三方账号信息成功");
		try {
			CreateUserReq cur = new CreateUserReq();
			cur = JUserCallService.initCreateUserBean(cur);
			int uid = JUserCallService.createUserCall(cur);
			BindThirdPartyUserReq bd = JUserCallService.initBindThirdUserReq(uid);
			bd.setThirdPartyType(ThirdPartyTypeEnum.WeiXin);
			Map<String, BindThirdPartyUserReq> m = new HashMap<String, BindThirdPartyUserReq>();
			m.put(bd.getThirdPartyId(), bd);
			JUserCallService.bindThirdPartyUserCall(bd);

			BindThirdPartyUserReq bd2 = JUserCallService.initBindThirdUserReq(uid);
			bd2.setThirdPartyType(ThirdPartyTypeEnum.Weibo);
			m.put(bd2.getThirdPartyId(), bd2);
			JUserCallService.bindThirdPartyUserCall(bd2);

			BindThirdPartyUserReq bd3 = JUserCallService.initBindThirdUserReq(uid);
			bd3.setThirdPartyType(ThirdPartyTypeEnum.Weibo);
			m.put(bd3.getThirdPartyId(), bd3);
			JUserCallService.bindThirdPartyUserCall(bd3);

			getUserThirdPartyAccountDtoBean.setUserId(uid);
			getUserThirdPartyAccountDtoCall.setData(getUserThirdPartyAccountDtoBean);
			getUserThirdPartyAccountDtoCall.callService();

			Logger.verifyEquals("true", getUserThirdPartyAccountDtoCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", getUserThirdPartyAccountDtoCall.getString("ResponseCode"), "验证ResponseCode");
			verifyDB(uid, m, getUserThirdPartyAccountDtoCall);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetUserThirdPartyAccountDto_005() {
		Logger.start(false, "验证未绑定第三方账号信息查询");
		try {
			CreateUserReq cur = new CreateUserReq();
			cur = JUserCallService.initCreateUserBean(cur);
			int uid = JUserCallService.createUserCall(cur);
			// BindThirdPartyUserReq
			// bd=JUserCallService.initBindThirdUserReq(uid);
			// bd.setThirdPartyType(ThirdPartyTypeEnum.WeiXin);
			// Map<String, BindThirdPartyUserReq> m=new HashMap<String,
			// BindThirdPartyUserReq>();
			// m.put(bd.getThirdPartyId(), bd);
			// JUserCallService.bindThirdPartyUserCall(bd);
			getUserThirdPartyAccountDtoBean.setUserId(uid);
			getUserThirdPartyAccountDtoCall.setData(getUserThirdPartyAccountDtoBean);
			getUserThirdPartyAccountDtoCall.callService();

			Logger.verifyEquals("true", getUserThirdPartyAccountDtoCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", getUserThirdPartyAccountDtoCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyEquals("ok", getUserThirdPartyAccountDtoCall.getString("SuccessMessage"), "验证SuccessMessage");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetUserThirdPartyAccountDto_006() {
		Logger.start(false, "验证绑定后解绑第三方账号信息查询");
		try {
			CreateUserReq cur = new CreateUserReq();
			cur = JUserCallService.initCreateUserBean(cur);
			int uid = JUserCallService.createUserCall(cur);
			BindThirdPartyUserReq bd = JUserCallService.initBindThirdUserReq(uid);
			bd.setThirdPartyType(ThirdPartyTypeEnum.WeiXin);
			Map<String, BindThirdPartyUserReq> m = new HashMap<String, BindThirdPartyUserReq>();
			m.put(bd.getThirdPartyId(), bd);
			JUserCallService.bindThirdPartyUserCall(bd);
			UnBindThirdPartyUserReq unbind = new UnBindThirdPartyUserReq();
			unbind.setUserId(uid);
			unbind.setThirdPartyType(bd.getThirdPartyType());
			JUserCallService.UnBindThirdPartyUserCall(unbind);
			getUserThirdPartyAccountDtoBean.setUserId(uid);
			getUserThirdPartyAccountDtoCall.setData(getUserThirdPartyAccountDtoBean);
			getUserThirdPartyAccountDtoCall.callService();

			Logger.verifyEquals("true", getUserThirdPartyAccountDtoCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", getUserThirdPartyAccountDtoCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyEquals("ok", getUserThirdPartyAccountDtoCall.getString("SuccessMessage"), "验证SuccessMessage");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	/**
	 * @param uid
	 * @param m
	 *            Map<String, BindThirdPartyUserReq> 绑定信息
	 * @param getUserThirdPartyAccountDtoCall2
	 */
	private void verifyDB(Integer uid, Map<String, BindThirdPartyUserReq> m,
			GetUserThirdPartyAccountDtoCall getUserThirdPartyAccountDtoCall2) {
		JsonArray resultlist = getUserThirdPartyAccountDtoCall.getJsonObject().get("Result").getAsJsonArray();

		Map<String, Map> m2 = new HashMap<String, Map>();
		for (JsonElement jsonElement : resultlist) {
			String thirdPartyId = jsonElement.getAsJsonObject().get("ThirdPartyId").getAsString();
			String thirdPartyType = jsonElement.getAsJsonObject().get("ThirdPartyType").getAsString();
//			String appSource = jsonElement.getAsJsonObject().get("appSource").getAsString();
			Boolean bind = jsonElement.getAsJsonObject().get("IsBind").getAsBoolean();
			Map map = new HashMap<>();
			map.put("thirdPartyId", thirdPartyId);
			map.put("thirdPartyType", thirdPartyType);
//			map.put("appSource", appSource);
			map.put("bind", bind);
			m2.put(thirdPartyId, map);
		}
		Logger.debug(m2);
		List<ThirdpartyUserbind> tublist = new JUserWapper().selectthirdpartyuserbind(uid);
		for (ThirdpartyUserbind thirdpartyUserbind : tublist) {
			BindThirdPartyUserReq bindinfo = m.get(thirdpartyUserbind.getThirdPartyId());
			Logger.comment("验证数据库");
			Logger.verifyNotNull(bindinfo, "查询到绑定信息");
			Logger.verifyEquals(bindinfo.getAppSource().getCode(), thirdpartyUserbind.getAppSource(), "验证AppSource");
			Logger.verifyEquals(bindinfo.getThirdPartyType().getCode(), thirdpartyUserbind.getThirdPartyType(),
					"验证ThirdPartyType");
			Logger.comment("验证返回结果");
			Map tempm = m2.get(thirdpartyUserbind.getThirdPartyId());
//			Logger.verifyEquals(bindinfo.getAppSource().getCode(), tempm.get("appSource"), "验证AppSource");
			Logger.verifyEquals(bindinfo.getThirdPartyType().toString(), tempm.get("thirdPartyType"),
					"验证ThirdPartyType");
			Logger.verifyEquals(true, tempm.get("bind"), "验证bind");
		}

	}
}